<<<<<<< HEAD
// logger.c
#include "sys_log.h"
#include <stdlib.h>
#ifdef __linux__
#include <syslog.h>
#endif

// 全局配置 - 使用新的枚举成员
static LogConfig g_config = {
    .level = LOG_LVL_INFO,
    .target = LOG_TARGET_CONSOLE,
    .filename = NULL,
    .use_color = 1
};

static FILE* log_file = NULL;

// ANSI颜色代码
static const char* level_colors[] = {
    "\033[36m", // NOTE: 青色
    "\033[32m", // INFO: 绿色
    "\033[33m", // WARN: 黄色
    "\033[31m", // ERROR: 红色
    "\033[35m"  // FATAL: 紫色
};

static const char* level_names[] = {
    "NOTE",
    "INFO",
    "WARN",
    "ERROR",
    "FATAL"
};

// 初始化日志系统
void log_init(LogConfig config) {
    g_config = config;
    
    if (g_config.target == LOG_TARGET_FILE && g_config.filename) {
        log_file = fopen(g_config.filename, "a");
        if (!log_file) {
            fprintf(stderr, "ERROR: Cannot open log file: %s\n", g_config.filename);
            g_config.target = LOG_TARGET_CONSOLE;
        }
    }
    
#ifdef __linux__
    if (g_config.target == LOG_TARGET_SYSLOG) {
        openlog(NULL, LOG_PID | LOG_CONS, LOG_USER);
    }
#endif
}

// 设置日志级别
void log_set_level(LogLevel level) {
    if (level >= LOG_LVL_NOTE && level < LOG_LVL_COUNT) {
        g_config.level = level;
    }
}

// 核心日志函数
void log_message(LogLevel level, const char* file, int line, const char* fmt, ...) {
    // 过滤低于设定级别的日志
    if (level < g_config.level) return;
    
    // 获取当前时间
    time_t now = time(NULL);
    struct tm *tm_info = localtime(&now);
    char timestamp[20];
    strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S", tm_info);
    
    // 提取文件名（不含路径）
    const char* base_file = strrchr(file, '/');
    base_file = base_file ? base_file + 1 : file;
    
    // 格式化日志消息
    char msg[512];
    va_list args;
    va_start(args, fmt);
    vsnprintf(msg, sizeof(msg), fmt, args);
    va_end(args);
    
    // 根据不同目标输出
    switch (g_config.target) {
        case LOG_TARGET_CONSOLE: {
            if (g_config.use_color) {
                printf("%s[%s] [%s] [%s:%d] %s\033[0m\n",
                       level_colors[level], timestamp, level_names[level], base_file, line, msg);
            } else {
                printf("[%s] [%s] [%s:%d] %s\n",
                       timestamp, level_names[level], base_file, line, msg);
            }
            fflush(stdout);
            break;
        }
        
        case LOG_TARGET_FILE: {
            if (log_file) {
                fprintf(log_file, "[%s] [%s] [%s:%d] %s\n",
                        timestamp, level_names[level], base_file, line, msg);
                fflush(log_file);
            }
            break;
        }
        
#ifdef __linux__
        case LOG_TARGET_SYSLOG: {
            int priority;
            switch(level) {
                case LOG_LVL_NOTE:    priority = LOG_NOTICE; break;
                case LOG_LVL_INFO:    priority = LOG_INFO;   break;
                case LOG_LVL_WARN:    priority = LOG_WARNING; break;
                case LOG_LVL_ERROR:   priority = LOG_ERR;    break;
                case LOG_LVL_FATAL:   priority = LOG_CRIT;   break;
                default:              priority = LOG_DEBUG;
            }
            syslog(priority, "[%s:%d] %s", base_file, line, msg);
            break;
        }
#endif
        
        case LOG_TARGET_NULL:
            // 不输出任何内容
            break;
    }
    
    // FATAL级别终止程序
    if (level == LOG_LVL_FATAL) {
        log_close();
        exit(EXIT_FAILURE);
    }
}

// 关闭日志系统
void log_close() {
    if (log_file) {
        fclose(log_file);
        log_file = NULL;
    }
    
#ifdef __linux__
    if (g_config.target == LOG_TARGET_SYSLOG) {
        closelog();
    }
#endif
=======
// logger.c
#include "sys_log.h"
#include <stdlib.h>
#ifdef __linux__
#include <syslog.h>
#endif

// 全局配置 - 使用新的枚举成员
static LogConfig g_config = {
    .level = LOG_LVL_INFO,
    .target = LOG_TARGET_CONSOLE,
    .filename = NULL,
    .use_color = 1
};

static FILE* log_file = NULL;

// ANSI颜色代码
static const char* level_colors[] = {
    "\033[36m", // NOTE: 青色
    "\033[32m", // INFO: 绿色
    "\033[33m", // WARN: 黄色
    "\033[31m", // ERROR: 红色
    "\033[35m"  // FATAL: 紫色
};

static const char* level_names[] = {
    "NOTE",
    "INFO",
    "WARN",
    "ERROR",
    "FATAL"
};

// 初始化日志系统
void log_init(LogConfig config) {
    g_config = config;
    
    if (g_config.target == LOG_TARGET_FILE && g_config.filename) {
        log_file = fopen(g_config.filename, "a");
        if (!log_file) {
            fprintf(stderr, "ERROR: Cannot open log file: %s\n", g_config.filename);
            g_config.target = LOG_TARGET_CONSOLE;
        }
    }
    
#ifdef __linux__
    if (g_config.target == LOG_TARGET_SYSLOG) {
        openlog(NULL, LOG_PID | LOG_CONS, LOG_USER);
    }
#endif
}

// 设置日志级别
void log_set_level(LogLevel level) {
    if (level >= LOG_LVL_NOTE && level < LOG_LVL_COUNT) {
        g_config.level = level;
    }
}

// 核心日志函数
void log_message(LogLevel level, const char* file, int line, const char* fmt, ...) {
    // 过滤低于设定级别的日志
    if (level < g_config.level) return;
    
    // 获取当前时间
    time_t now = time(NULL);
    struct tm *tm_info = localtime(&now);
    char timestamp[20];
    strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S", tm_info);
    
    // 提取文件名（不含路径）
    const char* base_file = strrchr(file, '/');
    base_file = base_file ? base_file + 1 : file;
    
    // 格式化日志消息
    char msg[512];
    va_list args;
    va_start(args, fmt);
    vsnprintf(msg, sizeof(msg), fmt, args);
    va_end(args);
    
    // 根据不同目标输出
    switch (g_config.target) {
        case LOG_TARGET_CONSOLE: {
            if (g_config.use_color) {
                printf("%s[%s] [%s] [%s:%d] %s\033[0m\n",
                       level_colors[level], timestamp, level_names[level], base_file, line, msg);
            } else {
                printf("[%s] [%s] [%s:%d] %s\n",
                       timestamp, level_names[level], base_file, line, msg);
            }
            fflush(stdout);
            break;
        }
        
        case LOG_TARGET_FILE: {
            if (log_file) {
                fprintf(log_file, "[%s] [%s] [%s:%d] %s\n",
                        timestamp, level_names[level], base_file, line, msg);
                fflush(log_file);
            }
            break;
        }
        
#ifdef __linux__
        case LOG_TARGET_SYSLOG: {
            int priority;
            switch(level) {
                case LOG_LVL_NOTE:    priority = LOG_NOTICE; break;
                case LOG_LVL_INFO:    priority = LOG_INFO;   break;
                case LOG_LVL_WARN:    priority = LOG_WARNING; break;
                case LOG_LVL_ERROR:   priority = LOG_ERR;    break;
                case LOG_LVL_FATAL:   priority = LOG_CRIT;   break;
                default:              priority = LOG_DEBUG;
            }
            syslog(priority, "[%s:%d] %s", base_file, line, msg);
            break;
        }
#endif
        
        case LOG_TARGET_NULL:
            // 不输出任何内容
            break;
    }
    
    // FATAL级别终止程序
    if (level == LOG_LVL_FATAL) {
        log_close();
        exit(EXIT_FAILURE);
    }
}

// 关闭日志系统
void log_close() {
    if (log_file) {
        fclose(log_file);
        log_file = NULL;
    }
    
#ifdef __linux__
    if (g_config.target == LOG_TARGET_SYSLOG) {
        closelog();
    }
#endif
>>>>>>> 39e3ba406c849a6599ada4a566e867047f276b22
}